package cn.edu.stu.syllabus.library.query;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.BindView;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.base.BaseFragment;
import cn.edu.stu.syllabus.repository.library.LibrarySearchInfo;
import cn.edu.stu.syllabus.datas.LibrarySearchCondition;
import cn.edu.stu.syllabus.library.list.QueryBooksListActivity;
import cn.edu.stu.syllabus.utils.ToastUtils;
import cn.edu.stu.syllabus.widgets.FixButtonSizeUtils;
import cn.edu.stu.syllabus.widgets.TextInputSpinner;

/**
 * Created by daidaijie on 2017/3/27.
 */

public class QueryBooksFragment extends BaseFragment {


    @BindView(R.id.keywordEditText)
    EditText mKeywordEditText;
    @BindView(R.id.queryWaysTextInputSpinner)
    TextInputSpinner mQueryWaysTextInputSpinner;
    @BindView(R.id.sortWaysTextInputSpinner)
    TextInputSpinner mSortWaysTextInputSpinner;
    @BindView(R.id.orderWaysTextInputSpinner)
    TextInputSpinner mOrderWaysTextInputSpinner;
    @BindView(R.id.startQueryButton)
    Button mStartQueryButton;

    public static QueryBooksFragment newInstance() {
        QueryBooksFragment fragment = new QueryBooksFragment();
        return fragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_query_books;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mQueryWaysTextInputSpinner.setItems(LibrarySearchCondition.getInstance().mQueryWays);
        mSortWaysTextInputSpinner.setItems(LibrarySearchCondition.getInstance().mSortWays);
        mOrderWaysTextInputSpinner.setItems(LibrarySearchCondition.getInstance().mOrderWays);

        FixButtonSizeUtils.fixButtonSize(mStartQueryButton);
        mStartQueryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkKeyWordsNoEmpty()) {
                    LibrarySearchInfo searchInfo = getCurrentSearchInfo();
                    Intent intent = QueryBooksListActivity.newIntent(mActivity, searchInfo);
                    mActivity.startActivity(intent);
                }
            }
        });
    }

    private boolean checkKeyWordsNoEmpty() {
        if (TextUtils.isEmpty(mKeywordEditText.getText().toString())) {
            ToastUtils.getInstance().showToast(ToastUtils.ToastType.WARNING, "关键字不能为空", Toast.LENGTH_SHORT);
            return false;
        }
        return true;
    }

    LibrarySearchInfo getCurrentSearchInfo() {
        LibrarySearchInfo librarySearchInfo = new LibrarySearchInfo();
        librarySearchInfo.setKeyword(mKeywordEditText.getText().toString());
        LibrarySearchCondition condition = LibrarySearchCondition.getInstance();
        librarySearchInfo.setQueryWay(
                condition.mQueryWayStringMap.get(mQueryWaysTextInputSpinner.getCurrentText()));
        librarySearchInfo.setSortWay(
                condition.mSortWaysStringMap.get(mSortWaysTextInputSpinner.getCurrentText()));
        librarySearchInfo.setOrderWay(
                condition.mOrderWaysStringMap.get(mOrderWaysTextInputSpinner.getCurrentText()));

        return librarySearchInfo;
    }


}
